var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teamList: [],

  },
  add(){
    wx.navigateTo({
      url: '/supervisor/pages/addpeople/addpeople?typeindex='+(this.data.worktype-1),
    })
  },
  // 25.瓦工列表
  getBricklayList() {
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    let t = this
    wx.request({
      url: app.globalData.Interfaceurl + 'getBricklayList',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,

      },
      success: res => {
        wx.hideLoading();
        //  
        let teamList
        if (this.data.bricklayList) {
          teamList= res.data.bricklayList.filter(item =>  item.workType == t.data.worktype && !t.data.checkoutList.some(item1 => item1 == item.userId))
        } else {
    
          teamList = res.data.bricklayList.filter(item => item.workType == t.data.worktype && !t.data.checkoutList.some(item1 => item1 == item.userId))
        }
        // teamList.map(item => item.isshow = false)
      
        teamList.forEach(item => {
          item.isshow = false
          
        })
        this.setData({
          teamList: teamList
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },



  showBtn(e) {
    let teamList = this.data.teamList
    let index = e.currentTarget.dataset.index
    if (this.data.worktype != 1) {
      teamList.forEach(item => item.isshow = false)
      teamList[index].isshow = !teamList[index].isshow
    } else {
      teamList[index].isshow = !teamList[index].isshow
    }

    this.setData({
      teamList: teamList
    })

  },

  determine() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    let workArr = prevPage.data.workArr
    // console.log(workArr[this.data.index].bricklayListest[this.data.index1])
    workArr[this.data.index].bricklayList = [...workArr[this.data.index].bricklayList,...this.data.teamList.filter(item => item.isshow == true)]




    let len = workArr[this.data.index].bricklayList.length
    if (workArr[this.data.index].accounType == 0) {
      let workNum = workArr[this.data.index].workNum

      workArr[this.data.index].price=workArr[this.data.index].bricklayUnivalent/len
      workArr[this.data.index].bricklayBudgetAmount = workNum *   workArr[this.data.index].price * len
    } else {
      let bigarea = workArr[this.data.index].bigarea
      workArr[this.data.index].price=workArr[this.data.index].bricklayUnivalent/len
      workArr[this.data.index].bricklayBudgetAmount = bigarea * workArr[this.data.index].price * len
    }


    let totalPrice = workArr.reduce(function (prev, cur) {
      return prev + cur.bricklayBudgetAmount
    }, 0);

    prevPage.setData({
      workArr: workArr,
      totalPrice: totalPrice
    })
    wx.navigateBack({
      delta: 1,
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let date = new Date()
    let mouth = date.getMonth() - 0 + 1 >= 10 ? date.getMonth() - 0 + 1 : "0" + (date.getMonth() - 0 + 1)
    let day = date.getDate() - 0 >= 10 ? date.getDate() : "0" + (date.getDate())

    this.setData({
      value: app.globalData.width * 2 * 0.122,
      today: date.getFullYear() + "-" + mouth + "-" + day,
      stateTime: date.getFullYear() + "-" + mouth + "-" + day,
      endTime: date.getFullYear() + "-" + mouth + "-" + day,
      worktype: options.worktype,
      index: options.index,

      checkoutList: JSON.parse(options.checkoutList)
    })
    if (options.bricklayList) {
      this.setData({
        bricklayList: JSON.parse(options.bricklayList),
      })
    }
    this.getBricklayList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})